def p(n):
    if n<=1:
        return False
    for i in range(2, int(n **0.5)+1):
        if n%i==0:
            return False
    return True
m=[]
d = int(input())
if 999<d<10000:
    b = 1
    pr= 10000
    for j in range(4):
        for i in range(4):
            if p(int(d%pr//b)) == True:
                   m.append(int(d%pr//b))
            if b<1000:
                b*=10
            else:
                b = 1
        if pr>10:
            pr/=10
        else:
            pr = 10000
print(max(m))
        
    
    


        
        
